<script setup>
import { ref, onMounted } from 'vue'
import { Search, View, Loading } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import request from '../utils/request'

// 用户数据
const users = ref([])
const loading = ref(false)
const searchKeyword = ref('')
const roleFilter = ref('')

// 分页数据
const pagination = ref({
  current: 1,
  pageSize: 10,
  total: 0,
  pages: 0
})

// 获取用户列表
const fetchUsers = async () => {
  loading.value = true
  try {
    const params = {
      page: pagination.value.current,
      limit: pagination.value.pageSize,
      username: searchKeyword.value,
      role: roleFilter.value
    }

    const response = await request.get('/users', { params })
    const result = response.data
    
    if (result.code === 200) {
      users.value = result.data.list
      pagination.value.total = result.data.total
      pagination.value.pages = result.data.pages
      pagination.value.current = result.data.page
      pagination.value.pageSize = result.data.limit
    } else {
      ElMessage.error(result.message || '获取用户列表失败')
    }
  } catch (error) {
    console.error('获取用户列表失败:', error)
    // 错误处理已在request拦截器中完成
  } finally {
    loading.value = false
  }
}

// 搜索处理
const handleSearch = () => {
  pagination.value.current = 1
  fetchUsers()
}

// 重置搜索
const resetSearch = () => {
  searchKeyword.value = ''
  roleFilter.value = ''
  pagination.value.current = 1
  fetchUsers()
}

// 分页处理
const handlePageChange = (page) => {
  pagination.value.current = page
  fetchUsers()
}

// 页面大小改变
const handleSizeChange = (size) => {
  pagination.value.pageSize = size
  pagination.value.current = 1
  fetchUsers()
}

// 状态标签类型
const getStatusType = (status) => {
  return status ? 'success' : 'danger'
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

onMounted(() => {
  fetchUsers()
})
</script>

<template>
  <div class="look-person-container">
    <div class="page-header">
      <h2>查看用户</h2>
      <p>查看所有用户的基本信息和状态</p>
    </div>
    
    <div class="content-card">
      <div class="card-header">
        <div class="header-left">
          <h3>用户列表</h3>
        </div>
        
        <div class="search-area">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索用户名、邮箱或电话"
            size="small"
            style="width: 300px; margin-right: 10px;"
            @keyup.enter="handleSearch"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
          
          <el-select
            v-model="roleFilter"
            placeholder="选择角色"
            size="small"
            style="width: 120px; margin-right: 10px;"
            clearable
          >
            <el-option label="老板" value="老板" />
            <el-option label="管理员" value="管理员" />
            <el-option label="员工" value="员工" />
          </el-select>
          
          <el-button type="primary" size="small" @click="handleSearch">
            搜索
          </el-button>
          
          <el-button size="small" @click="resetSearch">
            重置
          </el-button>
        </div>
      </div>
      
      <div class="table-container">
        <el-table 
          :data="users" 
          style="width: 100%" 
          border
          v-loading="loading"
          stripe
        >
          <el-table-column prop="username" label="用户名" width="120" />
          <el-table-column prop="email" label="邮箱" width="180" show-overflow-tooltip />
          <el-table-column prop="phone" label="电话" width="120" />
          <el-table-column prop="sex" label="性别" width="80" align="center" />
          <el-table-column prop="age" label="年龄" width="80" align="center" />
          <el-table-column prop="role" label="角色" width="100" align="center" />
          <el-table-column prop="status" label="状态" width="100" align="center">
            <template #default="scope">
              <el-tag :type="getStatusType(scope.row.status)">
                {{ scope.row.status ? '启用' : '禁用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180" align="center">
            <template #default="scope">
              {{ formatDate(scope.row.createTime) }}
            </template>
          </el-table-column>
        </el-table>
      </div>
      
      <!-- 分页 -->
      <div v-if="users.length > 0" class="pagination-container">
        <el-pagination
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pagination.pageSize"
          :current-page="pagination.current"
          @size-change="handleSizeChange"
          @current-change="handlePageChange"
        />
      </div>
    </div>
  </div>
</template>

<style scoped>
.look-person-container {
  min-height: calc(100vh - 140px);
  padding: 20px;
  background-color: #f5f7fa;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0 0 8px 0;
  color: #303133;
  font-size: 24px;
}

.page-header p {
  margin: 0;
  color: #909399;
  font-size: 14px;
}

.content-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
  min-height: calc(100vh - 200px);
}

.card-header {
  padding: 20px;
  border-bottom: 1px solid #ebeef5;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-left h3 {
  margin: 0;
  margin-right: 20px;
  font-size: 18px;
  font-weight: 500;
  color: #303133;
}

.search-area {
  display: flex;
  align-items: center;
}

.table-container {
  padding: 0 20px;
}

.pagination-container {
  padding: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>